const Router = require("koa-router");
const router = new Router();
const listData = require("../data/list.json");
const cityData = require("../data/city.json");
const { AlipaySdk } = require("alipay-sdk");
//科普号tab标题
const kepuhaotitle = [
  {
    id: 1,
    name: "精选",
  },
  {
    id: 2,
    name: "关注",
  },
  {
    id: 3,
    name: "订阅",
  },
];
//获取数据加搜索
router.get("/date", (ctx, body) => {
  const { currentPage = 1, pageSize = 10, wd, id, id2 } = ctx.query;
  let middleData = listData.data.list.concat([]);
  //接收id参数改变点赞数
  if (id) {
    middleData.forEach((item) => {
      //判断id是否相等
      if (item.id == id) {
        item.goodflag = !item.goodflag;
        console.log(item.goodflag);
        if (item.goodflag) {
          console.log(111);
          item.reply_num = item.reply_num + 1;
          console.log(item.reply_num);
        } else {
          console.log(222);
          item.reply_num = item.reply_num - 1;
          console.log(item.reply_num);
        }
      }
    });
  }
  //接收id2参数改变关注flag状态
  if (id2) {
    middleData.forEach((item) => {
      if (item.id == id2) {
        item.flag = !item.flag;
      }
    });
  }
  if (wd) {
    middleData = middleData.filter((item) => {
      return item.name.includes(wd);
    });
  }
  ctx.body = {
    code: 200,
    data: middleData.slice(
      (currentPage - 1) * pageSize,
      currentPage * pageSize
    ),
    message: "请求成功",
  };
});
//传入id获取详情接口
router.get("/dateid", (ctx, body) => {
  //接收params参数
  let id = ctx.query.id;
  console.log(id);

  //对象查找数据
  let data1 = null;
  listData.data.list.forEach((item) => {
    if (item.id.toString() === id) {
      data1 = item;
    }
  });
  console.log(data1);
  if (data1) {
    ctx.body = {
      code: 200,
      data: data1,
      message: "请求成功",
    };
  } else {
    ctx.body = {
      code: 400,
      message: "该条数据不存在",
      data: listData,
    };
  }
});
//标题
router.get("/kepuhaotitle", (ctx, body) => {
  ctx.body = {
    code: 200,
    data: kepuhaotitle,
  };
});

router.get('/city', (ctx) => {
    const name = ctx.request.query.name;
    const hospital = ctx.request.query.hospital;
    const keshi = ctx.request.query.keshi;
    const sex = ctx.request.query.sex;
    const address= ctx.request.query.address;
    const minAge = ctx.request.query.min_age? parseInt(ctx.request.query.min_age) : null;
    const maxAge = ctx.request.query.max_age? parseInt(ctx.request.query.max_age) : null;
    const doctors = listData['data']['list'];
    const result = [];
    for (const doctor of doctors) {
        // 根据姓名进行筛选
        if (name &&!doctor['name'].includes(name)) {
            continue;
        } 
        // 筛选为空结果为全部
        if(address)
      
        if (address &&!doctor['address'].includes(address)) {
            continue;
        }
       
        // 根据医院进行筛选
        if (hospital &&!doctor['hospital'].includes(hospital)) {
            continue;
        }
        // 根据科室进行筛选
        if (keshi &&!doctor['keshi'].includes(keshi)) {
            continue;
        }
        // 根据性别进行筛选
        if (sex && sex!== doctor['sex']) {
            continue;
        }
        // 根据年龄范围进行筛选
        if (minAge!== null && doctor['age'] < minAge) {
            continue;
        }
        if (maxAge!== null && doctor['age'] > maxAge) {
            continue;
        }
        result.push(doctor);
    }
    ctx.body = result;
  
});
// esmodule / typescript，可以使用 import AlipaySdk from 'alipay-sdk';
// 普通公钥模式
const alipaySdk = new AlipaySdk({
  appId: "9021000139668301",
  // keyType: 'PKCS1', // 默认值。请与生成的密钥格式保持一致，参考平台配置一节
  privateKey:
    "MIIEoAIBAAKCAQEAhrQH3o+UkJf1BOXTkxASTCY6uPJCc0nmqQpwn+x0qSJ99vv3L+alsyXx9RVosEVEVr87vIngRDkZU4uQkDJ9DLUU/0foKvJp4fZ17VlXBMl2xyQKBWT7rk8RKzL6domi0PoJJY1YRvRRUi9gJsQ5js419IwSJebdcn67SjzKCtDori4jYb/B9B2vEm8aiwwwWLtLrEuBUY9qHa6ieEYizQSI6V7XPUi+XXyfpqfVlGRXC+cFtsAKoCnrxuGETZp7nGugCeqSvG5ZanUTDAdyVPYVBkJx78SDWKFtgLQSEdVHwmF8ByVYc+0+2LkLynPUWGPVTywdrayGBHAmoE8MewIDAQABAoIBADP1vhNbzzL3asw42yQfTu+Qz5YH3Robp54NKOSHkXislWpwRylzDvfL60leuMNF9NaBbcmMZbXrUJmpfFojH5mHQFI4qpXmbHF9YH/ttmN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D70VP8CoXE7", // 支付宝私密钥
  alipayPublicKey:
    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhrQH3o+UkJf1BOXTkxASTCY6uPJCc0nmqQpwn+x0qSJ99vv3L+alsyXx9RVosEVEVr87vIngRDkZU4uQkDJ9DLUU/0foKvJp4fZ17VlXBMl2xyQKBWT7rk8RKzL6domi0PoJJY1YRvRRUi9gJsQ5js419IwSJebdcn67SjzKCtDori4jYb/B9B2vEm8aiwwwWLtLrEuBUY9qHa6ieEYizQSI6V7XPUi+XXyfpqfVlGRXC+cFtsAKoCnrxuGETZp7nGugCeqSvG5ZanUTDAdyVPYVBkJx78SDWKFtgLQSEdVHwmF8ByVYc+0+2LkLynPUWGPVTywdrayGBHAmoE8MewIDAQAB", // 应用公钥
  gateway: "https://openapi-sandbox.dl.alipaydev.com/gateway.do",
});
router.get("/zf", async (ctx) => {
  let {
    "obj[id]": id,
    "obj[total]": total,
    "obj[title]": title,
    "obj[des]": des,
  } = ctx.query;

  const bizContent = {
    out_trade_no: id,
    product_code: "FAST_INSTANT_TRADE_PAY",
    subject: title,
    body: des,
    total_amount: total,
  };

  // 支付页面接口，返回 html 代码片段，内容为 Form 表单
  const result = alipaySdk.pageExec("alipay.trade.page.pay", {
    method: "GET",
    bizContent,
    returnUrl: "http://localhost:5173/",
  });
  ctx.body = {
    code: 200,
    msg: "支付成功",
    url: result,
  };
});
//websorket
const qustionAnswerPairs = {
  "你好": "您好呀!很高兴能和你聊天",
  "今天天气如何": "我没办法获取实时天气信息哦,您可以查看天气预报",
  "你会做什么": "我可回答一些简单的问题,陪你聊聊天",
  "再见": "再见,下次再见",
  "你叫什么名字": "我是傻逼，是您的智能助手哦。",
  "你能给我找一下药品吗?": "好的，请问您需要找什么药品呢？",
  "你吃完饭了吗?": "我是个程序，没有吃和不吃之分哦。",
};
// 处理聊天请求的路由
router.post("/char", (ctx, body) => {
  const userQuestion =  ctx.request.body.question;
  const answer = qustionAnswerPairs[userQuestion] || "对不起，我不知道哦";
  ctx.body = {
    code: 200,
    data: {
      answer,
    },
    message: "请求成功",
  };
});
module.exports = router;
